<template>
    <div class="turntrue">
      <div class="account">
        <p class="account_tip">账户余额将提现到微信</p>
        <span class="turn_money border-bottom">
          <p>&yen;</p>
          <input type="number" v-model="turntruewallet">
        </span>
        <span class="allmoney">
          <p>账户余额:  &yen; {{wallet}}</p>
            <router-link to="detailofaccount">
          <p>账户明细</p>
              </router-link>
        </span>
      </div>
      <span class="turnbtm" @click="turntrue">
          提现到微信
      </span>
    </div>
</template>

<script>
import { Dialog } from 'vant'
import Vue from 'vue'
import { Toast } from 'vant'
import * as fetch from '@/api/fetch'
Vue.use(Toast)
Vue.use(Dialog)

export default {
  name: 'TurnTrue',
  data () {
    return {
      turntruewallet:"",
      wallet: ''
    }
  },

  methods: {
    async getUserBalance () {
      const res = await fetch.getUserBalance({
      })
      console.log(res)
      if (res.errcode == 0) {
        this.wallet = res.data.wallet
      }
    },

    async extractWallet () {
      const res = await fetch.extractWallet({
        balance:this.turntruewallet
      })
      console.log(res)
    },

    turntrue () {
      var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
      if(reg.test(this.turntruewallet)){
        if(this.turntruewallet == "" ||this.turntruewallet == 0){
          Toast('请输入提现金额');
          return false
        }
        if(this.turntruewallet > this.wallet){
          Dialog.alert({
            title: '提示',
            message: '可提现金额不足'
          }).then(() => {
            // on close
          })
          return false
        }
        // Toast.success('提现成功')
        this.extractWallet()
      }else{
        Toast('请输入正确的提现金额');
        return false
      }
    }
  },
  mounted () {
    this.getUserBalance()
  },
  activated() {
    this.turntruewallet = ""
  }
}
</script>

<style lang="less" scoped>
  @import '~less/variable.less';
  @import '~less/mixins.less';
  .turntrue{
    .account{
      width: 92%;
      margin-left: 4%;
      height: 175px;
      padding: 20px 0;
      border-radius: 10px;
      margin-top: 20px;
      box-shadow:0px 2px 9px 0px rgba(105,105,145,0.3);
      .account_tip{
        margin-left: 20px;
        color: #858b95;
      }
      .turn_money{
        margin-left: 20px;
        margin-right: 20px;
        height: 70px;
        margin-top: 30px;
        display: flex;flex-direction: row;
        align-items: center;
        font-weight: bold;
        p{
          margin-left: 0;
          font-size: 35px;
        }
        input{
          border: none;
          width: 80%;
          margin-left: 20px;
          font-size: 40px;
        }
      }
      .allmoney{
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        margin-left: 20px;
        margin-right: 20px;
        margin-top: 40px;
        color: #858b95;
        p:last-child{
          color: #4492ff;
        }
      }
    }
    .turnbtm{
      display: block;
      width: 92%;
      margin-left: 4%;
      height: 50px;
      background: #187a72;
      margin-top: 30px;
      line-height: 50px;
      text-align: center;
      color: white;
      font-size: 18px;
      border-radius: 25px;
    }
  }
</style>
